package com.cupk.controller;

import com.cupk.pojo.Result;
import com.cupk.pojo.User;
import com.cupk.pojo.UserWrapper;
import com.cupk.service.UserService;
import com.cupk.utils.Md5Util;
import com.cupk.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    public Result register(@RequestBody User user, @RequestHeader(value = "Admin_Authorization") String admin_password) {
        if (!admin_password.equals("CUPK_JZY_13040559761")) {
            return Result.error("您没有权限使用该接口！");
        }
        userService.register(user);
        return Result.success();
    }

    @PostMapping("/login")
    public Result<UserWrapper> login(@RequestBody Map<String, Object> input) {
        String username = (String) input.get("username");
        String password = (String) input.get("password");
        System.out.println(username);
        User user = userService.getUserByAccount(username);
        if (user == null) {
            return Result.error("用户名或密码错误");
        }
        String truePassword = user.getPassword();
        if (!truePassword.equals(Md5Util.getMD5String(password))) {
            return Result.error("用户名或密码错误");
        }
        String token = userService.generateToken(user);
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(token, token, 24, TimeUnit.HOURS);
        Integer flag = 0;
        if (truePassword.equals(Md5Util.getMD5String(username))) {
            flag = 1;
        }
        UserWrapper response = new UserWrapper(token, flag, user);
        return Result.success(response);
    }

    @GetMapping("/generateCode")
    public Result generateCode(@RequestParam String email, String username) {
        String verifyCode = userService.generateCode();
        User old_user = userService.getUserByEmail(email);
        User new_user = userService.getUserByAccount(username);
        if (new_user == null && old_user == null) {
            return Result.error("请求非法！");
        }
        User user;
        if (new_user != null && new_user.getEmail() == null) {
            user = new_user;
        } else {
            user = old_user;
        }
        if (user == null) {
            return Result.error("请求非法！");
        }
        Integer id = user.getId();
        String userName = user.getName();
        String userId = Integer.toString(id);
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(email, userId, 60, TimeUnit.SECONDS);
        operations.set(userId, verifyCode, 60, TimeUnit.SECONDS);
        userService.sendEmail(userName, email, verifyCode);
        return Result.success();
    }

    @PostMapping("/reset")
    public Result reset(@RequestBody Map<String, Object> input) {
        String new_password = (String) input.get("newPassword");
        String email = (String) input.get("email");
        String verify_code = (String) input.get("verifyCode");
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String userId = operations.get(email);
        if (userId == null) {
            return Result.error("尚未发送邮箱验证码！");
        }
        String right_code = operations.get(userId);
        if (!right_code.equals(verify_code)) {
            return Result.error("邮箱验证码不正确！");
        }
        userService.updatePassword(userId, new_password);
        userService.updateEmail(userId, email);
        return Result.success();
    }
}
